package vn.edu.fpt.xml.hrm.controller.approval;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.naming.NamingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;

import vn.edu.fpt.xml.hrm.common.AppConstant;
import vn.edu.fpt.xml.hrm.common.CheckUtil;
import vn.edu.fpt.xml.hrm.common.FormatUtil;
import vn.edu.fpt.xml.hrm.controller.AbstractController;
import vn.edu.fpt.xml.hrm.model.ActivityBean;
import vn.edu.fpt.xml.hrm.model.EmployeeBean;
import vn.edu.fpt.xml.hrm.model.ProjectBean;
import vn.edu.fpt.xml.hrm.pojo.Activity;
import vn.edu.fpt.xml.hrm.pojo.Employee;
import vn.edu.fpt.xml.hrm.pojo.Log02010Response;
import vn.edu.fpt.xml.hrm.pojo.Log02010Response.Activities;
import vn.edu.fpt.xml.hrm.pojo.Log02010Response.Employees;
import vn.edu.fpt.xml.hrm.pojo.Log02010Response.Projects;
import vn.edu.fpt.xml.hrm.pojo.Project;

public class ListActivitiesController extends AbstractController {

    /**
     *
     */
    private static final long serialVersionUID = 3251181563123293843L;

    @Override
    protected String execute() throws SQLException, ClassNotFoundException, DatatypeConfigurationException, JAXBException, IOException, NamingException {
        String search = null;
        Log02010Response log02010Response = null;
        JAXBContext jaxb = null;
        Marshaller m = null;
        List<Project> proList = null;
        List<Activity> actList = null;
        if (getRequest().getSession().getAttribute("USER") != null) {
            search = getRequest().getParameter("search");
            if ("all".equals(search)) {
                log02010Response = new Log02010Response();
                log02010Response.setProjects(new Projects());
                proList = getProList();
                if (proList != null && proList.size() > 0) {
                    log02010Response.getProjects().getProject().addAll(proList);
                    actList = getActList(proList);
                } else {
                    log02010Response.getProjects().getProject();
                }

                log02010Response.setActivities(new Activities());
                if (actList != null && actList.size() > 0) {
                    log02010Response.getActivities().getActivity().addAll(actList);
                } else {
                    log02010Response.getActivities().getActivity();
                }
                log02010Response.setEmployees(new Employees());
                log02010Response.getEmployees().getEmployee().addAll(getEmpList());
                jaxb = JAXBContext.newInstance(log02010Response.getClass());
                m = jaxb.createMarshaller();
                m.marshal(log02010Response, getResponse().getOutputStream());
                getResponse().setContentType("text/xml");
                setForward(false);
                setRedirect(false);
            } else if ("activities".equals(search)) {
				log02010Response = new Log02010Response();
				log02010Response.setProjects(new Projects());
				log02010Response.setActivities(new Activities());
				log02010Response.setEmployees(new Employees());
				proList = getProList();
				actList = getActList(proList);

				if (actList != null && actList.size() > 0) {
					log02010Response.getActivities().getActivity()
							.addAll(actList);
				} else {
					log02010Response.getActivities().getActivity();
				}
				jaxb = JAXBContext.newInstance(log02010Response.getClass());
				m = jaxb.createMarshaller();
				m.marshal(log02010Response, getResponse().getOutputStream());
				getResponse().setContentType("text/xml");
				setForward(false);
				setRedirect(false);
            }
            return AppConstant.URL_LOG_02010;
        }
        setForward(false);
        setRedirect(true);
        return "index.jsp";
    }

    private List<Project> getProList() throws SQLException, ClassNotFoundException, DatatypeConfigurationException, NamingException {
        List<Project> proList = null;
        ProjectBean projectBean = null;
//		User user = null;
        //TODO get user id from session
        projectBean = new ProjectBean();
//		user = (User) getRequest().getSession().getAttribute("user");
        proList = projectBean.getProjectList(Integer.parseInt((String) getRequest().getSession().getAttribute("EMPLOYEEID")));
//		proList = projectBean.getProjectList(5);
        return proList;
    }

    private List<Employee> getEmpList() throws SQLException, ClassNotFoundException, DatatypeConfigurationException, NamingException {
        List<Employee> empList = null;
        EmployeeBean employeeBean = null;
        employeeBean = new EmployeeBean();
        empList = employeeBean.getEmployeeList();
        return empList;
    }

    private List<Activity> getActList(List<Project> proList) throws SQLException, ClassNotFoundException, DatatypeConfigurationException, NamingException {
        List<Activity> actList = null;
        ActivityBean activityBean = null;
		String fromYear = null;
		String fromMonth = null;
		String fromDay = null;
        Date fromDate = null;
		String toYear = null;
		String toMonth = null;
		String toDay = null;
        Date toDate = null;
		String projectIdS = null;
		String employeeIdS = null;
        int projectId = 0;
        int employeeId = 0;
        String status = null;
		String search = null;
		Calendar calendar = null;
		search = getRequest().getParameter("search");
		if (search != null) {
			// get input parameter
			fromYear = getRequest().getParameter("fromYear");
			fromMonth = getRequest().getParameter("fromMonth");
			fromDay = getRequest().getParameter("fromDay");
			toYear = getRequest().getParameter("toYear");
			toMonth = getRequest().getParameter("toMonth");
			toDay = getRequest().getParameter("toDay");
			projectIdS = getRequest().getParameter("proId");
			employeeIdS = getRequest().getParameter("empId");
			status = getRequest().getParameter("status");
			// process input
			if (!CheckUtil.isBlankOrNull(fromYear)
					&& !CheckUtil.isBlankOrNull(fromMonth)
					&& !CheckUtil.isBlankOrNull(fromDay)) {
				fromDate = FormatUtil
						.stringToDate(fromYear, fromMonth, fromDay);
			}
			if (!CheckUtil.isBlankOrNull(toYear)
					&& !CheckUtil.isBlankOrNull(toMonth)
					&& !CheckUtil.isBlankOrNull(toDay)) {
				toDate = FormatUtil.stringToDate(toYear, toMonth, toDay, "23",
						"59", "59");
			}
		} else {
			// setup initial search condition
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -14);
			fromDate = calendar.getTime();
		}
        activityBean = new ActivityBean();
        actList = activityBean.getApproveList(fromDate, toDate, projectIdS, proList, employeeIdS, status);
        return actList;
    }
}
